﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WXYW.DAL;
using WXYW.BLL;

namespace WXYW
{
    public class ContextService
    {
        public static User CurrentAccount
        {
            get
            {
                if (HttpContext.Current.Session["CurrentAccount"] == null)
                {
                    UserService userService = new UserService();
                    HttpContext.Current.Session["CurrentAccount"] = userService.GetUserByName(HttpContext.Current.User.Identity.Name);
                }
                return HttpContext.Current.Session["CurrentAccount"] as User;
            }
            set
            {
                HttpContext.Current.Session["CurrentAccount"] = value;
            }
        }

        public static void ClearAccount()
        {
            HttpContext.Current.Session["CurrentUser"] = null;
            HttpContext.Current.Session.Abandon();
        }

        public static int GetPermission(int menuID)
        {
            if (menuID > 0)
            {
                UserService userService = new UserService();
                return userService.GetPermission(menuID, CurrentAccount.UserType.ID);
            }
            //else if (ContextService.CurrentAccount.UserType.ID == (int)RoleType.Administrator)
            //{
            //    return 2;
            //}
            else
            {
                return 0;
            }
        }

        public static int HasSubPermission(int parentMenuID)
        {
            if (parentMenuID > 0)
            {
                UserService userService = new UserService();
                return userService.HasPermissionOfSubMenu(parentMenuID, CurrentAccount.UserType.ID);
            }
            else
            {
                return 0;
            }
        }
    }
}
